package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.domain.SysRole;
import com.syf.mapper.SysPrivilegeMapper;
import com.syf.mapper.SysRoleMapper;
import com.syf.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
*
*@author 苏耀奋
*@create 2021-07-11 12:11
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper; //角色mapper

    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper ; //权限mapper


    /**
     * 判断一个用户是否为超级的管理员
     *
     * @param userId
     * @return
     */
    @Override
    public boolean isSuperAdmin(Long userId) {
        //当用户的角色code 为：ROLE_ADMIN 时，该用户为超级的管理员
        String roleCode = sysRoleMapper.getUserRoleCode(userId);  //用户的id->用户的角色
        if (!StringUtils.isEmpty(roleCode) && roleCode.equals("ROLE_ADMIN")) {
            return true;
        }
        return false;
    }

    /**
     * 使用角色的名称模糊分页角色查询
     *      page(E page, Wrapper<T> queryWrapper)
     *      like(boolean condition, R column, Object val) 参数是否为空，数据库字段，参数值
     * @param page 分页数据
     * @param name 角色的名称
     * @return
     */
    @Override
    public Page<SysRole> findByPage(Page<SysRole> page, String name) {
        return page(page,new LambdaQueryWrapper<SysRole>().like(
                !StringUtils.isEmpty(name),
                SysRole::getName,
                name
        ));
    }

    /*
    *//**
     * 根据ID查询
     *
     * @param id 主键ID
     *//*
    @Override
    public SysRole getById(Serializable id) {
        SysRole sysRole = super.getById(id);  //根据ID查询角色(this.getBaseMapper().selectById(id))
        sysRole.setPrivileges(sysPrivilegeMapper.selectByRoleId(sysRole.getId()));
        return sysRole;
    }
    */
}
